package ru.spb.osll.myfit.client.dialog;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;


public class AddContentDialog extends InputDialog {

  public AddContentDialog(DialogEventListener listener) {
    super(listener);
    setText("Enter content:");
    defineItems();
  }

  public void defineItems() {
    m_cbButtonOk.setText("Add");
    m_cbButtonOk.setEnabled(false);
    m_cbButtonOk.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        if (!m_suggestBox.getText().isEmpty()) {
          m_listener.onOK(m_suggestBox.getText(), m_sourceEvent);
        }
        hide();
        m_sourceEvent = null;
      }
    });

    m_suggestBox.addKeyUpHandler(new KeyUpHandler() {
      
      @Override
      public void onKeyUp(KeyUpEvent arg0) {
        try {
          int level = Integer.parseInt(m_suggestBox.getText().trim());
          if (level <= 0) {
            m_cbButtonOk.setEnabled(false);
          } else {
            m_cbButtonOk.setEnabled(true);
          }
        } catch (NumberFormatException e) {
          m_cbButtonOk.setEnabled(false);
        }
      }
    });
  }
  
  @Override
  public void hide() {
    m_suggestBox.setText("");
    m_cbButtonOk.setEnabled(false);
    super.hide();
  }
}
